package cn.iocoder.yudao.framework.excel.core.util;


import com.alibaba.excel.enums.CellDataTypeEnum;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {

    private static final int MAX_COLUMN_WIDTH = 255 * 256;
    private static final int MIN_COLUMN_WIDTH = 2 * 256;

    private final int maxColumnWidth;
    private final int minColumnWidth;



    public CustomColumnWidthStyleStrategy(int minBytes,int maxBytes) {
        this.maxColumnWidth = Math.min(calWidth(maxBytes), MAX_COLUMN_WIDTH);
        this.minColumnWidth = Math.max(calWidth(minBytes), MIN_COLUMN_WIDTH);
    }

    private static int calWidth(int bytes) {
        return bytes * 256 * 2 / 3;
    }

    public CustomColumnWidthStyleStrategy() {
        this.maxColumnWidth = MAX_COLUMN_WIDTH;
        this.minColumnWidth = MIN_COLUMN_WIDTH;
    }

    private Map<Integer, Map<Integer, Integer>> cache = new HashMap<Integer, Map<Integer, Integer>>(8);

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head,
                                  Integer relativeRowIndex, Boolean isHead) {
        boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        if (!needSetWidth) {
            return;
        }
        Map<Integer, Integer> maxColumnWidthMap = cache.computeIfAbsent(writeSheetHolder.getSheetNo(), k -> new HashMap<Integer, Integer>(16));
        Integer columnWidth = dataLength(cellDataList, cell, isHead);
        if (columnWidth < 0) {
            return;
        }
        if (columnWidth > maxColumnWidth) {
            columnWidth = maxColumnWidth;
        }
        if (columnWidth < minColumnWidth) {
            columnWidth = minColumnWidth;
        }
        Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
        if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
            maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth);
        }
    }

    private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
        if (isHead) {
            //不考虑标题
            return -1;
        }
        CellData cellData = cellDataList.get(0);
        CellDataTypeEnum type = cellData.getType();
        if (type == null) {
            return minColumnWidth;
        }
        switch (type) {
            case STRING:
                return calWidth(cellData.getStringValue().getBytes().length);
            case BOOLEAN:
                return calWidth(cellData.getBooleanValue().toString().getBytes().length);
            case NUMBER:
                return calWidth(cellData.getNumberValue().toString().getBytes().length );
            default:
                return minColumnWidth;
        }
    }
}

